<?php

namespace app\api\controller\fruit;

use app\admin\model\FffAcc;
use app\admin\model\FishingAcc;
use app\admin\model\FruitWarriorUser;
use app\api\controller\Base;
use think\Request;
use app\api\model\fruit\warrior\User;
use think\cache\driver\Redis;

/**
 * @author zero
 * @Time 2019/1/30
 *
 * Class Warrior
 * @package app\api\controller\fruit
 *
 * 切水果勇士基类
 */
class BaseWarrior extends Base
{

    /**
     * @var array 无需登录的方法
     */
    protected $noNeedLogin = [];

    /**
     * @var User 用户实例
     */
    protected $user;

    /**
     * @var string 审核控制版本号
     */
    protected $control_version = '0.0.0';

    /**
     * @var string 当前版本号
     */
    protected $version;

    /**
     * @var int 特殊关卡每一个水果价格
     */
    protected $each_fruit_price = 8;

    /**
     * @var int 特殊关卡增加的钻石数
     */
    protected $special_customs_diamonds = 40;

    /**
     * @var int 每小时离线收益
     */
    protected $offline_income = 500;

    /**
     * @var int 炫耀有奖奖励
     */
    protected $share_award = 6;

    /**
     * @var int 每隔几关赠送无尽模式入场券
     */
    protected $ticket_level = 20;

    /**
     * @var array 首页展示的跳转其它小程序
     */
    protected $important_list = array(
        array(
            'name' => '游趣娱乐',
            'appid' => 'wxa9002f27f3ed1519',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/game/icon/you.jpg'
        ),
        array(
            'name' => '萌消团',
            'appid' => 'wxd26399f7e0a403d0',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/mxt.png'
        ),
        array(
            'name' => '一起来合体',
            'appid' => 'wxd382be58d8c6d6c9',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/heti.png'
        ),
        array(
            'name' => '一起飞飞飞',
            'appid' => 'wxe42ac5dd0f116b26',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/fly.jpg'
        ),
        array(
            'name' => '拯救查尔斯',
            'appid' => 'wxc76853fdcf188c3e',
            'icon' => 'https://xcx-1256535092.cos.ap-shanghai.myqcloud.com/jingpinhezi/icon/charles.jpg'
        )
    );

    /**
     * 构造方法
     * @access public
     * @param Request $request Request 对象
     */
    public function __construct(Request $request = null)
    {
        header('Access-Control-Allow-Origin:*'); // 跨域
        parent::__construct($request);
        $this->version = $request->param('version') ?: '0.0.0';
    }

    /**
     * 检测当前控制器和方法是否匹配传递的数组
     *
     * @param array $arr 需要验证权限的数组
     * @return boolean
     */
    public function match($arr = [], $actionname)
    {
        $arr = is_array($arr) ? $arr : explode(',', $arr);
        if (!$arr) {
            return FALSE;
        }
        $arr = array_map('strtolower', $arr);
        // 是否存在
        if (in_array(strtolower($actionname), $arr) || in_array('*', $arr)) {
            return TRUE;
        }

        // 没找到匹配
        return FALSE;
    }

    /**
     * 获取指定数量的不重复随机数
     * @param $min int 随机最小值
     * @param $max int 随机最大值
     * @param $num int 随机数数量
     * @return array|null
     */
    public function unique_rand($min, $max, $num)
    {
        $count = 0;
        $return_arr = array();
        while ($count < $num) {
            $return_arr[] = mt_rand($min, $max);
            $return_arr = array_flip(array_flip($return_arr));
            $count = count($return_arr);
        }
        shuffle($return_arr);
        return $return_arr;
    }

    /**
     * 假的中奖者头像昵称
     * @return string
     * @throws \Exception
     */
    public function fake_lucky_info()
    {
        // 天天取钓鱼中取出头像数据
        $avatar_url_list = FishingAcc::whereNotNull('avatar_url')
            ->where('avatar_url', '<>', '')
            ->whereNotLike('avatar_url', '% %')
            ->column('avatar_url');
        $avatar_url_id_list = array_rand($avatar_url_list, 10);
        // 一起飞飞飞中取出昵称数据
        $not_like = [
            '%爸%', '%妈%', '%爷%', '%奶%', '%姥%', '%姥爷%', '%单元%', '%13%', '%14%', '%15%', '%16%', '%17%',
            '%18%', '%@%', '%%', '%(%', '%[%', '%？%', '%?%', '% %', '%。%', '%（%', '%wxid%', '%12345%',
            '%-%', '%，%', '%%', '%省%', '%市%', '%公司%', '%中国%', '%百度%', '%销售%', '%经理%', '%培训%'
        ];
        $nick_name_list = FffAcc::whereNotNull('nick_name')
            ->where('nick_name', '<>', '')
            ->whereNotLike('nick_name', $not_like)
            ->column('nick_name');
        $nick_name_id_list = array_rand($nick_name_list, 10);

        // 假的中奖者电话号码
        $header = array(
            130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 150, 151, 152, 153, 155, 156,
            157, 158, 159, 176, 177, 178, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189
        );
        $tail = $this->unique_rand(1000, 9999, 10);

        $user = new FruitWarriorUser();
        $list = [
            ['id' => 1, 'nick_name' => $nick_name_list[$nick_name_id_list[0]], 'avatar_url' => $avatar_url_list[$avatar_url_id_list[0]], 'endless_score' => 0, 'phone_number' => $header[array_rand($header)] . '****' . $tail[0]],
            ['id' => 2, 'nick_name' => $nick_name_list[$nick_name_id_list[1]], 'avatar_url' => $avatar_url_list[$avatar_url_id_list[1]], 'endless_score' => 0, 'phone_number' => $header[array_rand($header)] . '****' . $tail[1]],
            ['id' => 3, 'nick_name' => $nick_name_list[$nick_name_id_list[2]], 'avatar_url' => $avatar_url_list[$avatar_url_id_list[2]], 'endless_score' => 0, 'phone_number' => $header[array_rand($header)] . '****' . $tail[2]],
            ['id' => 4, 'nick_name' => $nick_name_list[$nick_name_id_list[3]], 'avatar_url' => $avatar_url_list[$avatar_url_id_list[3]], 'endless_score' => 0, 'phone_number' => $header[array_rand($header)] . '****' . $tail[3]],
            ['id' => 5, 'nick_name' => $nick_name_list[$nick_name_id_list[4]], 'avatar_url' => $avatar_url_list[$avatar_url_id_list[4]], 'endless_score' => 0, 'phone_number' => $header[array_rand($header)] . '****' . $tail[4]],
            ['id' => 6, 'nick_name' => $nick_name_list[$nick_name_id_list[5]], 'avatar_url' => $avatar_url_list[$avatar_url_id_list[5]], 'endless_score' => 0, 'phone_number' => $header[array_rand($header)] . '****' . $tail[5]],
            ['id' => 7, 'nick_name' => $nick_name_list[$nick_name_id_list[6]], 'avatar_url' => $avatar_url_list[$avatar_url_id_list[6]], 'endless_score' => 0, 'phone_number' => $header[array_rand($header)] . '****' . $tail[6]],
            ['id' => 8, 'nick_name' => $nick_name_list[$nick_name_id_list[7]], 'avatar_url' => $avatar_url_list[$avatar_url_id_list[7]], 'endless_score' => 0, 'phone_number' => $header[array_rand($header)] . '****' . $tail[7]],
            ['id' => 9, 'nick_name' => $nick_name_list[$nick_name_id_list[8]], 'avatar_url' => $avatar_url_list[$avatar_url_id_list[8]], 'endless_score' => 0, 'phone_number' => $header[array_rand($header)] . '****' . $tail[8]],
            ['id' => 10, 'nick_name' => $nick_name_list[$nick_name_id_list[9]], 'avatar_url' => $avatar_url_list[$avatar_url_id_list[9]], 'endless_score' => 0, 'phone_number' => $header[array_rand($header)] . '****' . $tail[9]],
        ];
        $user->saveAll($list);
        return 'ok';
    }

    /**
     * 假的中奖者分数
     * @return string
     * @throws \think\exception\DbException
     */
    public function fake_lucky_score()
    {
        $max_score = FruitWarriorUser::max('endless_score') + 1;
        $score_index = [1, 1, 1, 1, 1, 0, 0, 0, 0, 0];
        shuffle($score_index);
        for ($i = 0; $i < 10; $i++) {
            $user = FruitWarriorUser::get($i + 1);
            if ($user && $score_index[$i] == 1) {
                $user->level += mt_rand(2, 6);
                if ($user->endless_score < $max_score) {
                    $user->endless_score = $max_score + mt_rand(23, 372);
                }
                $user->save();
            }
        }
        return 'ok';
    }

    /**
     * 每周处理排名
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function handle_week_rank()
    {
        User::where('week_lucky', '<>', 0)->update(['week_lucky' => 0]);
        $list = User::order('endless_score', 'desc')->limit(5)->select();
        foreach ($list as $key => $item) {
            $arr[] = ['nick_name' => $item->nick_name, 'avatar_url' => $item->avatar_url, 'phone_number' => $item->phone_number];
            $item->week_lucky = $key + 1;
            $item->save();
        }
        $redis = new Redis();
        $redis->set('warrior_fruit_week_lucky', json_encode($arr));
        User::where('endless_score', '<>', 0)->update(['endless_score' => 0]);
        return 'ok';
    }

}
